<?php
declare (strict_types = 1);

namespace app\admin\controller\delivery\channel;

use app\admin\traits\Actions;
use app\common\model\delivery\ShopGoods;
use think\annotation\route\Group;
use think\annotation\route\Route;
use think\facade\Db;

#[Group("delivery/channel-goods")]
class Goods extends ChannelBase
{
    protected $model;

    use Actions{
        add as private _add;
        edit as private _edit;
    }

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ShopGoods();
        $this->recyclebinColumns=[
            'id'=>'ID',
            'name'=>'商品名称',
        ];
        $this->recyclebinColumnsType=[
            'id'=>'text',
            'name'=>'text',
        ];
        $this->assign('goodstype',ShopGoods::GOODSTYPE);
    }

    #[Route('GET,JSON','index')]
    public function index()
    {
        if (false === $this->request->isAjax()) {
            return $this->fetch();
        }
        $where = [];
        $where[] = ['channel_id','=',$this->channel->id];
        if($this->request->post('selectpage')){
            return $this->selectpage($where);
        }
        [$where, $order, $limit, $with] = $this->buildparams($where);
        $list = $this->model
            ->with(['coupon'])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    #[Route('GET,POST','add')]
    public function add()
    {
        if($this->request->isPost()){
            $this->postParams['channel_id']=$this->channel->id;
        }
        return $this->_add();
    }

    #[Route('GET,POST','edit')]
    public function edit()
    {
        if($this->request->isPost()){
            $this->postParams['channel_id']=$this->channel->id;
        }
        return $this->_edit();
    }

    #[Route('GET,POST,JSON','recyclebin')]
    public function recyclebin($action='list')
    {
        switch ($action){
            case 'list':
                if (false === $this->request->isAjax()) {
                    $search=[];
                    $this->assign('search', implode(',',$search));
                    $this->assign('columns', $this->recyclebinColumns);
                    $this->assign('columnsType', $this->recyclebinColumnsType);
                    return $this->fetch('common/recyclebin');
                }
                $where = [];
                $where[] = ['channel_id','=',$this->channel->id];
                [$where, $order, $limit, $with] = $this->buildparams($where);
                $list = $this->model
                    ->withJoin($with,'left')
                    ->onlyTrashed()
                    ->where($where)
                    ->order($order)
                    ->paginate($limit);
                $result = ['total' => $list->total(), 'rows' => $list->items()];
                return json($result);
            case 'restore':
                $ids=$this->request->param('ids');
                foreach ($ids as $id){
                    $row=$this->model->onlyTrashed()->where(['id'=>$id,'channel_id'=>$this->channel->id])->find();
                    if($row){
                        $row->restore();
                    }
                }
                $this->success();
            case 'destroy':
                $ids=$this->request->param('ids');
                foreach ($ids as $id){
                    $row=$this->model->onlyTrashed()->where(['id'=>$id,'channel_id'=>$this->channel->id])->find();
                    if($row){
                        $row->force()->delete();
                    }
                }
                $this->success();
            case 'restoreall':
                $where=[];
                $where[]=['channel_id','=',$this->channel->id];
                $where[]=['deletetime','<>',null];
                $this->model->onlyTrashed()->where($where)->update(['deletetime'=>null]);
                $this->success();
            case 'clear':
                Db::execute('delete from '.$this->model->getTable().' where channel_id='.$this->channel->id.' and deletetime is not null');
                $this->success();
        }
    }
}
